﻿package
{
	public class Miscreant extends Sprites
	{
		import flash.events.Event;
		import flash.net.URLRequest;
		import flash.media.Sound;
		import flash.media.SoundChannel;
		private var distance:Number;
		private var xDifference:Number;
		private var yDifference:Number;
		private var xSign:Number;
		private var ySign:Number;
		private var hitSound:Sound;
		private var movingRight:Boolean;
		public var movingUp:Boolean;
		public var caughtTheFruit:Boolean;
		public var escaped:Boolean;
		
		private var hitSoundChannel:SoundChannel;
		
		public function Miscreant(size:Number,separation:Number,left:Number,right:Number,up:Number,down:Number)
		{
			distance = separation;
			initial_sprite_dir = "front";
			hitSound = new Sound(new URLRequest("Audio/hit.mp3"));
			hitSoundChannel = new SoundChannel();
			movingUp = false;
			movingRight = true;
			super(size,left,right,up,down);
			initial_y = 480;
			initial_x = Math.ceil(Math.random() * 800);
			setX(initial_x);
			setY(initial_y);
			caughtTheFruit = false;
			escaped = false;
			
		}
		public function hitTrees(syllabusCopies:Array):void
		{
			var syllabusCounter:int;
			syllabusCounter = 0;
			for (syllabusCounter=0; syllabusCounter<syllabusCopies.length;syllabusCounter = syllabusCounter + 1)
			{
				if  (this.hitTestObject(syllabusCopies[syllabusCounter])) 
				{
					caughtTheFruit = true;
					this.addChild(syllabusCopies[syllabusCounter]);
					
					// set the coordinates for the syllabus item to zero
					syllabusCopies[syllabusCounter].x = 0;
					syllabusCopies[syllabusCounter].y = 0;
					
				}
			}
		}
		public function moveMiscreant(syllabusCopies:Array)
		{
			hitTrees(syllabusCopies);
			if (caughtTheFruit == true)
			{
				if (this.y>=constraint_down)
				{					
					escaped = true;
					return;
				}
			}
			 
			
			if (movingRight == true)
			{
				this.x = this.x + moveStepSize;
				
				if (this.x >=constraint_right)
				{
					movingRight = false;
				}
				
			}
			
			else if (movingRight == false)
			{
				this.x = this.x - moveStepSize;
				
				if (this.x <=constraint_left)
				{
					movingRight = true;
				}
			}
			
			if (movingUp == true)
			{
				
				this.y = this.y - moveStepSize;
				
				if (this.y <= constraint_up)
				{
					movingUp = false;
				}
			}
			
			else if (movingUp == false)
			{
				this.y = this.y + moveStepSize;
				
				if (this.y >= constraint_down)
				{
					movingUp = true;
				}
			}
			
		}
		
		public function moveTowardsTarget(target:Sprites):void
		{
			xSign = 0;
			ySign = 0;
			xDifference = this.getX() - target.getX();
			yDifference = this.getY()+this.height - (target.getY()+target.height);
			if (xDifference!=0)
				xSign = xDifference/Math.abs(xDifference);
			if (yDifference!=0)
				ySign = yDifference/Math.abs(yDifference);
			
			if (Math.abs(xDifference)<= distance && Math.abs(yDifference) <= distance && FarmGame.boyIsHit==false && FarmGame.boyIsMoving==true)
			{
				var ran:Number = Math.floor(Math.random()*2);
				if (ran == 0) {
					xSign = 0;
				} else {
					ySign = 0;
				}
				
				if(this.moveStepSize < Math.abs(xDifference))
					this.x = this.x - xSign*this.moveStepSize;
				else
					this.x = this.x - xSign*Math.abs(xDifference);
					
				if(this.moveStepSize < Math.abs(yDifference))
					this.y = this.y - ySign*this.moveStepSize;
				else
					this.y = this.y - ySign*Math.abs(yDifference);
				
				if(checkHitTarget()==true)
				{
					FarmGame.boyIsHit = true;
					hitSoundChannel = hitSound.play(0,5);
					target.blink(this);
				}
			}
		}
		
		public function checkHitTarget():Boolean
		{
			if(Math.abs(xDifference)<3 && Math.abs(yDifference)<3)
				return true;
			else
				return false;
		}
	}
}